import { useEffect, useRef, useState } from 'react';

// 定时间隔
export const useInterval = (interval: number) => {
  const initSecond = 1;
  const ref = useRef<NodeJS.Timeout>();
  const [seconds, setSeconds] = useState(initSecond);
  useEffect(() => {
    ref.current = setTimeout(() => {
      if (seconds < interval) {
        setSeconds((prev) => prev + 1);
      } else {
        setSeconds(initSecond);
      }
    }, 1000);
    return () => clearTimeout(ref.current);
  }, [seconds, interval]);
  return seconds;
};
